/***********************************************************************
 * Application :   ATDL3 - Air Transport Data Link
 * Author:         Belgian Ministry Of Defence - CCCIS
 * Module:         DateTimeHelper.java
 * Purpose:        Defines the Class DateTimeHelper
 ***********************************************************************/

package be.donovan.gui.helper;

import java.util.Calendar;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Interval;
import org.joda.time.base.AbstractInstant;
import org.joda.time.base.AbstractPartial;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;

/**
 * helper class to transform date, times ...
 */
public class DateTimeHelper {

	/**
	 * returns the localTime from a zoulou time
	 * 
	 * @param time
	 * @param timeZone
	 */
	public static DateTime getLocalTime(DateTime zoulouTime, String timeZone) {
		return new DateTime(zoulouTime.getMillis() + DateTimeZone.forID(timeZone).getOffset(zoulouTime.getMillis()));
	}

	/**
	 * returns the zoulouTime from a localTime
	 * 
	 * @param zoulouTime
	 * @param timeZone
	 *            String from the list of JodaTime!
	 */
	public static DateTime getZoulouTime(DateTime localTime, String timeZone) {
		return new DateTime(localTime.getMillis() - DateTimeZone.forID(timeZone).getOffset(localTime.getMillis()));
	}

	public static String toZouluoDTG(AbstractInstant dt) {
		DateTimeFormatter fmt = new DateTimeFormatterBuilder().appendDayOfMonth(2).appendHourOfDay(2)
				.appendMinuteOfHour(2).appendLiteral('Z').appendMonthOfYearShortText().appendYear(1, 1).toFormatter();
		return dt.toString(fmt);
	}

	public static String toYearMonthDay(AbstractPartial dt) {
		DateTimeFormatter fmt = new DateTimeFormatterBuilder().appendDayOfMonth(2).appendLiteral(' ')
				.appendMonthOfYearShortText().appendLiteral(' ').appendYear(1, 1).toFormatter();
		return (dt == null) ? null : dt.toString(fmt);
	}

	/**
	 * returns year in YY
	 */
	public static String getYear() {
		DateTime dt = new DateTime();
		DateTimeFormatter fmt = new DateTimeFormatterBuilder().appendTwoDigitYear(2).toFormatter();
		return dt.toString(fmt);
	}

	/**
	 * returns UTC (zoulou time) of this moment
	 * 
	 * @return
	 */
	public static DateTime getUTCNow() {
		Calendar cal = Calendar.getInstance();
		return new DateTime(cal.getTimeInMillis() - cal.getTimeZone().getOffset(cal.getTimeInMillis()));
	}

	/**
	 * Used by the ACManager and the ActivityManager
	 * 
	 * @param beginTime
	 * @param endTime
	 * @param activityBeginTime
	 * @param activityEndTime
	 * @return
	 */
	public static Boolean isTimeOverlapping(Interval interval, DateTime activityBeginTime, DateTime activityEndTime) {
		boolean timeNotPossible1 = (interval.getEnd().getMillis() >= activityBeginTime.getMillis() && interval.getEnd()
				.getMillis() <= activityEndTime.getMillis());
		boolean timeNotPossible2 = (interval.getStart().getMillis() >= activityBeginTime.getMillis() && interval
				.getStart().getMillis() <= activityEndTime.getMillis());
		boolean timeNotPossible3 = (interval.getEnd().getMillis() > activityEndTime.getMillis() && interval.getStart()
				.getMillis() < activityBeginTime.getMillis());
		return (timeNotPossible1 || timeNotPossible2 || timeNotPossible3) ? true : false;
	}
}
